package com.atstudy.file;

import java.io.FileWriter;
import java.io.IOException;

/**
 * @version v1.0
 * @description:
 * @author: admin
 * @date: 2022/12/28 13:56
 * @copyright: 博为峰
 */
public class FileWriterDemo {
    public static void main(String[] args) {
//        m1();
//        m2();
//        try-with-resource 语法糖  Closeable接口的实现类
        try (FileWriter fileWriter = new FileWriter("C:\\Users\\admin\\Desktop\\java29\\io\\f01.txt", true);) {
            fileWriter.write("def");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void m2() {
        FileWriter fileWriter = null;
        try {
            int i = 1 / 0;
            fileWriter = new FileWriter("C:\\Users\\admin\\Desktop\\java29\\io\\f01.txt");

            fileWriter.write("def");

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileWriter != null) {
                try {
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private static void m1() throws IOException {
        //创建一个写字符数据的文件流对象,如果文件不存在，则创建，如果文件存在，则覆盖
        FileWriter fileWriter = new FileWriter("C:\\Users\\admin\\Desktop\\java29\\io\\f01.txt");
        //写入字符数据
        fileWriter.write("def");
        //关闭流
        fileWriter.close();
    }
}
